#include <stdio.h>
#include <stdlib.h>
#include <gccore.h>
#include <ogcsys.h>
#include <unistd.h>
#include <wiiuse/wpad.h>
#include <time.h>

#define gotoXY(x,y) printf("\x1b[%i;%iH", x, y)

//Prototypes

int genereNombre(int MIN, int MAX);
void compare(int nombreEntre, int nombreMystere);

static void *xfb = NULL;
static GXRModeObj *rmode = NULL;


//---------------------------------------------------------------------------------
int main(int argc, char **argv) 

{
//---------------------------------------------------------------------------------

	// Initialise the video system
	VIDEO_Init();
	
	// This function initialises the attached controllers
	WPAD_Init();
	
	// Obtain the preferred video mode from the system
	// This will correspond to the settings in the Wii menu
	rmode = VIDEO_GetPreferredMode(NULL);

	// Allocate memory for the display in the uncached region
	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
	
	// Initialise the console, required for printf
	console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
	
	// Set up the video registers with the chosen mode
	VIDEO_Configure(rmode);
	
	// Tell the video hardware where our display memory is
	VIDEO_SetNextFramebuffer(xfb);
	
	// Make the display visible
	VIDEO_SetBlack(FALSE);

	// Flush the video register changes to the hardware
	VIDEO_Flush();

	// Wait for Video setup to complete
	VIDEO_WaitVSync();
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();


	// The console understands VT terminal escape codes
	// This positions the cursor on row 2, column 0
	// we can use variables for this with format codes too
	// e.g. printf ("\x1b[%d;%dH", row, column );
	printf("\x1b[2;2H");
	
	
	// JEU PLUS OU MOINS
	
	
	int nombreEntre=0;
	int choix=0;
	int nombreMystere = 0;
	
	const int MAX = 100, MIN = 1;
				
		
	nombreMystere = genereNombre(MIN, MAX);	
	
	printf("Entrez le nombre Mystere:\n");
	

	while(nombreMystere != nombreEntre)
	{
		WPAD_ScanPads();
		u32 pressed = WPAD_ButtonsDown(0);
		
		
		
		if (pressed & WPAD_BUTTON_PLUS)
		{
			choix++;
			printf("\x1b[3;2H");
			printf("%d", choix);
		}
		
		if (pressed & WPAD_BUTTON_MINUS)
		{
			choix--;
			printf("\x1b[3;2H");
			printf("%d", choix);
		}
		
		if (pressed & WPAD_BUTTON_A)
		{
			nombreEntre=choix;
			printf("\x1b[4;2H");
			printf("Vous avez choisi %d\n", nombreEntre);
			
			compare(nombreEntre, nombreMystere);
		}
				
		if ( pressed & WPAD_BUTTON_HOME ) exit(0);
		
		
	}
	
	
	
	while(1)
	{
		WPAD_ScanPads();
		u32 pressed = WPAD_ButtonsDown(0);
		
		printf("\x1b[6;2H");
		printf("Credit (1) "); 
		
		if ( pressed & WPAD_BUTTON_1 )
		{
			printf("\x1b[7;2H");
			printf("Jeu ecrit par Silvering");
		}
		
		printf("\x1b[8;2H");
		printf("Quitter (Home) "); 
				
		if ( pressed & WPAD_BUTTON_HOME )
		{
			printf("\x1b[9;2H");
			printf("Chargement ..."); 
			exit(0);
		}	
		VIDEO_WaitVSync();
	}
	
	//FIN JEU PLUS OU MOINS

	return 0;
}

//Fonction Random
int genereNombre(int MIN, int MAX)
{
	int nombreMystere;
	srand(time(NULL));
	nombreMystere = (rand() % (MAX - MIN + 1)) + MIN;
		
	return nombreMystere;
}
//Fonction Compare
void compare(int nombreEntre, int nombreMystere)
{
		
		
	if (nombreEntre > nombreMystere)
		{
			printf("\x1b[5;2H");
			printf("C'est moins!"); 
		}
		else if (nombreEntre < nombreMystere)
		{
			printf("\x1b[5;2H");
			printf("C'est plus!"); 
		}
		else
		{
			printf("\x1b[5;2H");
			printf("Bien joue le nombre mystere etait, %d!", nombreMystere); 
		}
			
			
}
	
